package com.example.learn.controller;

import com.example.learn.pojo.FrontKnow;
import com.example.learn.pojo.Product;
import com.example.learn.service.KnowldService;
import com.example.learn.service.ProductService;
import com.example.learn.utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @GetMapping("/list")
    public R<Map> getProductList(String productName, String orderNo, String inventory) {
        List<Product> productList = productService.getProductList(productName, orderNo, inventory);
        Map dataMap = new HashMap();
        dataMap.put("data", productList);
        dataMap.put("total", productList.size());
        return R.success(dataMap,"success");
    }

    @PostMapping("/add")
    public R addProduct(@RequestBody Product product) {
        int count = productService.addProduct(product);
        if(count >= 1){
            return R.success("添加成功");
        }else {
            return R.failed("添加失败");
        }
    }

    @PostMapping("/update")
    public R updateProduct(@RequestBody Product product) {
        int count = productService.updateProduct(product);
        if(count >= 1){
            return R.success("修改成功");
        }else {
            return R.failed("修改失败");
        }
    }

    @GetMapping("/delete")
    public R delProductById(String id) {
        int count = productService.delProductById(id);
        if(count >= 1){
            return R.success("删除成功");
        }else {
            return R.failed("删除失败");
        }
    }
}
